package com.easy.ui.query;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.easy.R;
import com.easy.base.activity.BaseActivity;
import com.easy.bean.AbQueryBean;
import com.easy.tool.ToastUtils;
import com.easy.ui.query.adapter.CodeAdapter;
import com.easy.zxing.MipcaActivityCapture;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QueryActivity extends BaseActivity<QueryModel, QueryView, QueryPresent> implements QueryView {
    private String tag = "QueryActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.etcode)
    EditText etcode;
    @Bind(R.id.bt)
    Button bt;
    @Bind(R.id.bts)
    Button bts;
    @Bind(R.id.tvtype)
    TextView tvtype;
    @Bind(R.id.tvpcode)
    TextView tvpcode;
    @Bind(R.id.tvhcode)
    TextView tvhcode;
    @Bind(R.id.tvxcode)
    TextView tvxcode;
    @Bind(R.id.tvzcode)
    TextView tvzcode;
    @Bind(R.id.listview)
    ListView listview;
    private String code = "";
    private CodeAdapter codeAdapter = null;
    private final int SCANNIN= 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);
        ButterKnife.bind(this);
        tvTitle.setText("查询数码");
        tvright.setText("扫一扫");
        Drawable drawable = getResources().getDrawable(R.mipmap.scanfcode);
        drawable.setBounds(0,0,50,50);
        tvright.setCompoundDrawables(drawable,null,null,null);
        tvright.setCompoundDrawablePadding(10);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public QueryModel createModel() {
        return new QueryModelImpl();
    }

    @Override
    public QueryView createView() {
        return this;
    }

    @Override
    public QueryPresent createPresenter() {
        return new QueryPresent();
    }

    @Override
    public void query(AbQueryBean abQueryBean) {
        tvtype.setText("数码类型："+abQueryBean.getData().getCodeType());
        Drawable drawable1 = getResources().getDrawable(R.mipmap.type);
        drawable1.setBounds(0,0,50,50);
        tvtype.setCompoundDrawables(drawable1,null,null,null);
        tvtype.setCompoundDrawablePadding(10);

        tvpcode.setText("瓶码："+abQueryBean.getData().getPingCode());
        Drawable drawable2 = getResources().getDrawable(R.mipmap.pt);
        drawable2.setBounds(0,0,50,50);
        tvpcode.setCompoundDrawables(drawable2,null,null,null);
        tvpcode.setCompoundDrawablePadding(10);

        tvhcode.setText("盒码："+abQueryBean.getData().getBoxCode());
        Drawable drawable3 = getResources().getDrawable(R.mipmap.ht);
        drawable3.setBounds(0,0,50,50);
        tvhcode.setCompoundDrawables(drawable3,null,null,null);
        tvhcode.setCompoundDrawablePadding(10);

        tvxcode.setText("箱码："+abQueryBean.getData().getCaseCode());
        Drawable drawable4 = getResources().getDrawable(R.mipmap.xt);
        drawable4.setBounds(0,0,50,50);
        tvxcode.setCompoundDrawables(drawable4,null,null,null);
        tvxcode.setCompoundDrawablePadding(10);

        tvzcode.setText("此箱包含的盒码：");
        Drawable drawable5 = getResources().getDrawable(R.mipmap.zt);
        drawable5.setBounds(0,0,50,50);
        tvzcode.setCompoundDrawables(drawable5,null,null,null);
        tvzcode.setCompoundDrawablePadding(10);

        codeAdapter = new CodeAdapter(this,abQueryBean.getData().getListSub());
        listview.setAdapter(codeAdapter);
    }

    public void dataClear()
    {
        tvtype.setText("");
        tvtype.setCompoundDrawables(null,null,null,null);
        tvpcode.setText("");
        tvpcode.setCompoundDrawables(null,null,null,null);
        tvhcode.setText("");
        tvhcode.setCompoundDrawables(null,null,null,null);
        tvxcode.setText("");
        tvxcode.setCompoundDrawables(null,null,null,null);
        tvzcode.setText("");
        tvzcode.setCompoundDrawables(null,null,null,null);
        codeAdapter = null;
        listview.setAdapter(codeAdapter);
    }

    @OnClick({R.id.iv_title_left, R.id.tvright, R.id.bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tvright:
                Intent intent1 = new Intent();
                intent1.setClass(this, MipcaActivityCapture.class);
                startActivityForResult(intent1, SCANNIN);
                break;
            case R.id.bt:
                code = etcode.getText().toString();
                if (code.length() == 0) {
                    ToastUtils.showSingleToast(this, "请输入数码");
                    return;
                }
                dataClear();
                presenter.query(code);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCANNIN:
                if (resultCode == Activity.RESULT_OK) {
                    dataClear();
                    code = data.getStringExtra("result");
                    try {
                        int a= code.lastIndexOf("/");
                        etcode.setText(code .substring(a+1));
                        presenter.query(code .substring(a+1));
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        showMsg("条码不正确");
                    }
                }
                break;
        }
    }
}
